package sample.controller.todo;

import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.util.StringUtil;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;

public class DeleteController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(DeleteController.class.getName());

    @Override
    public Navigation run() {
        String key = key();
        if (!StringUtil.isEmpty(key)) {

            DatastoreService datastoreService =
                DatastoreServiceFactory.getDatastoreService();
            
            
            Transaction transaction = datastoreService.beginTransaction();
            try {
                datastoreService.delete(transaction, KeyFactory.stringToKey(key));
                transaction.commit();
            } finally {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
            }
        }
        return redirect(basePath);
    }
}
